﻿Public Class SavingClass

    Public Function GetSettingItem(ByVal File As String, ByVal Identifier As String) As String
        Dim S As New IO.StreamReader(File) : Dim Result As String = ""
        Do While (S.Peek <> -1)
            Dim Line As String = S.ReadLine
            If Line.ToLower.StartsWith(Identifier.ToLower & ":") Then
                Result = Line.Substring(Identifier.Length + 2)
            End If
        Loop
        S.Dispose()
        Return Result
    End Function

    Public Shared Function SaveSettings(ByVal PathToSave As String) As String
        Try
            Dim FullPath As String = PathToSave & "\settings.txt"
            Dim Lines As Integer
            Dim aryText(4) As String
            aryText(0) = "ProfileSelected: " & My.Settings.ProfileSelected
            aryText(1) = "Profile1Path: " & My.Settings.Profile1Path
            aryText(2) = "Profile2Path: " & My.Settings.Profile2Path
            aryText(3) = "FirefoxPath: " & My.Settings.FirefoxPath
            aryText(4) = "CCleanerPath: " & My.Settings.CCleanerPath
            Dim objWriter As New System.IO.StreamWriter(FullPath, False)
            For Lines = 0 To 4
                objWriter.WriteLine(aryText(Lines))
            Next
            objWriter.Close()
            Return "OK"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    Public Shared Function LoadSettings(ByVal PathToLoad As String) As String
        Dim SavingClass As New SavingClass
        Try
            My.Settings.ProfileSelected = SavingClass.GetSettingItem(PathToLoad & "\settings.txt", "ProfileSelected")
            My.Settings.Profile1Path = SavingClass.GetSettingItem(PathToLoad & "\settings.txt", "Profile1Path")
            My.Settings.Profile2Path = SavingClass.GetSettingItem(PathToLoad & "\settings.txt", "Profile2Path")
            My.Settings.FirefoxPath = SavingClass.GetSettingItem(PathToLoad & "\settings.txt", "FirefoxPath")
            My.Settings.CCleanerPath = SavingClass.GetSettingItem(PathToLoad & "\settings.txt", "CCleanerPath")
            Return "OK"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    Public Shared Function SaveAccount(ByVal PathToSave As String) As String
        Try
            Dim FullPath As String = PathToSave & "\accounts.txt"
            Dim Lines As Integer
            Dim aryText(3) As String
            aryText(0) = My.Settings.CurrentAccount & "1: " & My.Settings.FullName
            aryText(1) = My.Settings.CurrentAccount & "2: " & My.Settings.Email
            aryText(2) = My.Settings.CurrentAccount & "3: " & My.Settings.Password
            aryText(3) = String.Empty
            Dim objWriter As New System.IO.StreamWriter(FullPath, True)
            For Lines = 0 To 3
                objWriter.WriteLine(aryText(Lines))
            Next
            objWriter.Close()
            Return "OK"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    Public Shared Function LoadAccount(ByVal PathToLoad As String) As String
        Dim SavingClass As New SavingClass
        Try
            My.Settings.Email = SavingClass.GetSettingItem(PathToLoad & "\accounts.txt", My.Settings.CurrentAccount & "2")
            My.Settings.Password = SavingClass.GetSettingItem(PathToLoad & "\accounts.txt", My.Settings.CurrentAccount & "3")
            Return "OK"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    Public Shared Function SaveURL(ByVal PathToSave As String) As String
        Try
            Dim FullPath As String = PathToSave & "\urls.txt"
            Dim Lines As Integer
            Dim aryText(0) As String
            aryText(0) = My.Settings.CurrentAccount & My.Settings.CurrentVideo & ": " & My.Settings.CurrentURL
            Dim objWriter As New System.IO.StreamWriter(FullPath, True)
            For Lines = 0 To 0
                objWriter.WriteLine(aryText(Lines))
            Next
            objWriter.Close()
            Return "OK"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    Public Shared Function LoadURL(ByVal PathToLoad As String) As String
        Dim SavingClass As New SavingClass
        Try
            My.Settings.CurrentURL = SavingClass.GetSettingItem(PathToLoad & "\urls.txt", My.Settings.CurrentAccount & My.Settings.CurrentVideo)
            Return "OK"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function
End Class
